using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[NetworkSettings(channel = 0, sendInterval = 1f)]
public class PlayersManager : NetworkBehaviour
{
	public float VersionCheckDelay = 5f;

	[SyncVar(hook = "OnDataChanged")]
	public string PlayersData = string.Empty;

	public List<string> TeamsList = new List<string>();

	public List<PlayerData> PlayerList = new List<PlayerData>();

	public string NetworkPlayersData
	{
		get
		{
			return PlayersData;
		}
		[param: In]
		set
		{
			ref string playersData = ref PlayersData;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnDataChanged(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref playersData, 1u);
		}
	}

	private void Awake()
	{
		ClearPlayers();
	}

	public void ClearPlayers()
	{
		PlayerList.Clear();
		TeamsList.Clear();
	}

	public PlayerData GetPlayerData(int ID)
	{
		foreach (PlayerData player in PlayerList)
		{
			PlayerData current = player;
			if (current.ID == ID)
			{
				return current;
			}
		}
		return default(PlayerData);
	}

	public void AddPlayer(int id)
	{
		PlayerData item = default(PlayerData);
		item.Dead = 0;
		item.ID = id;
		item.Name = string.Empty;
		item.Team = string.Empty;
		PlayerList.Add(item);
	}

	private void AddTeam(string team)
	{
		if (!TeamsList.Contains(team))
		{
			TeamsList.Add(team);
		}
	}

	public void RemovePlayer(int id)
	{
		UnityEngine.Debug.Log(id + " Removed");
		int num = 0;
		while (true)
		{
			if (num < PlayerList.Count)
			{
				PlayerData playerData = PlayerList[num];
				if (id == playerData.ID)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		PlayerList.RemoveAt(num);
		rewritePlayersData();
	}

	public void UpdatePlayerInfo(int id, int score, int dead, string name, string team, string gameKey, bool isconnected)
	{
		bool flag = false;
		foreach (PlayerData player in PlayerList)
		{
			PlayerData current = player;
			if (current.ID == id)
			{
				flag = true;
			}
		}
		if (!flag)
		{
			AddPlayer(id);
		}
		PlayerData value = default(PlayerData);
		value.Dead = dead;
		value.ID = id;
		value.Name = name;
		value.Team = team;
		AddTeam(team);
		int num = 0;
		while (true)
		{
			if (num < PlayerList.Count)
			{
				PlayerData playerData = PlayerList[num];
				if (id == playerData.ID)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		PlayerList[num] = value;
		rewritePlayersData();
	}

	public void AddScore(int id, int score, int dead)
	{
		int num = 0;
		while (true)
		{
			if (num < PlayerList.Count)
			{
				PlayerData playerData = PlayerList[num];
				if (playerData.ID == id)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		PlayerData value = default(PlayerData);
		PlayerData playerData2 = PlayerList[num];
		value.Dead = playerData2.Dead;
		PlayerData playerData3 = PlayerList[num];
		value.ID = playerData3.ID;
		PlayerData playerData4 = PlayerList[num];
		value.Name = playerData4.Name;
		PlayerData playerData5 = PlayerList[num];
		value.Team = playerData5.Team;
		PlayerData playerData6 = PlayerList[num];
		value.Score = playerData6.Score + score;
		PlayerData playerData7 = PlayerList[num];
		value.Dead = playerData7.Dead + dead;
		PlayerList[num] = value;
		rewritePlayersData();
	}

	private void rewritePlayersData()
	{
		string text = string.Empty;
		string text2 = string.Empty;
		string text3 = string.Empty;
		string text4 = string.Empty;
		string text5 = string.Empty;
		string text6 = string.Empty;
		foreach (PlayerData player in PlayerList)
		{
			PlayerData current = player;
			if (current.ID != -1)
			{
				text = text + current.ID + ",";
				text2 = text2 + current.Team + ",";
				text5 = text5 + current.Score + ",";
				text3 = text3 + current.Dead + ",";
				text4 = text4 + current.Name + ",";
			}
		}
		foreach (string teams in TeamsList)
		{
			text6 = text6 + teams + ",";
		}
		NetworkPlayersData = text + "|" + text2 + "|" + text5 + "|" + text3 + "|" + text4 + "|" + text6;
	}

	public void ReadData(string data)
	{
		PlayerList.Clear();
		TeamsList.Clear();
		string[] array = data.Split("|"[0]);
		if (array.Length < 6)
		{
			return;
		}
		string[] array2 = array[0].Split(","[0]);
		string[] array3 = array[1].Split(","[0]);
		string[] array4 = array[2].Split(","[0]);
		string[] array5 = array[3].Split(","[0]);
		string[] array6 = array[4].Split(","[0]);
		string[] array7 = array[5].Split(","[0]);
		for (int i = 0; i < array2.Length; i++)
		{
			if (array2[i] != string.Empty)
			{
				PlayerData item = default(PlayerData);
				int.TryParse(array2[i], out item.ID);
				int.TryParse(array4[i], out item.Score);
				int.TryParse(array5[i], out item.Dead);
				item.Name = array6[i];
				item.Team = array3[i];
				PlayerList.Add(item);
			}
		}
		for (int j = 0; j < array7.Length; j++)
		{
			AddTeam(array7[j]);
		}
	}

	public void OnDataChanged(string data)
	{
		NetworkPlayersData = data;
		ReadData(data);
	}

	private void UNetVersion()
	{
	}

	public override float GetNetworkSendInterval()
	{
		return 1f;
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(PlayersData);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(PlayersData);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			PlayersData = reader.ReadString();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			OnDataChanged(reader.ReadString());
		}
	}
}
